package com.schoolegard.services.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.schoolegard.services.ParamServerService;
import com.schoolgard.dao.ParamServerDao;
import com.schoolgard.dao.ParamServerToDeviceDao;
import com.schoolgard.entitybean.GridDataModel;
import com.schoolgard.entitybean.ParamServer;
import com.schoolgard.entitybean.ParamServerToDevice;

public class ParamServerServiceImpl implements ParamServerService {

	// 系统用户
	private ParamServerDao paramServerDao;
	private ParamServerToDeviceDao serverToDeviceDao;

	public void setServerToDeviceDao(ParamServerToDeviceDao serverToDeviceDao) {
		this.serverToDeviceDao = serverToDeviceDao;
	}

	public void setParamServerDao(ParamServerDao paramServerDao) {
		this.paramServerDao = paramServerDao;
	}

	@Override
	public ParamServer findByDeviceId(String deviceId) {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ParamServer.class);
		criteria.add(Restrictions.eq("device.guid", deviceId));
		List<ParamServer> ps = paramServerDao.findByCriteria(criteria);
		return (ps != null && ps.size() > 0) ? ps.get(0) : null;
	}

	@Override
	public void saveOrUpdate(ParamServer paramServer) {
		paramServerDao.createOrModify(paramServer);
	}

	@Override
	public void saveParamAndJob(ParamServer paremServer, ParamServerToDevice job) {
		saveOrUpdate(paremServer);
		serverToDeviceDao.createOrModify(job);
	}
	
	public GridDataModel<ParamServerToDevice> findAllJob(
			ParamServerToDevice serverToDevice, int first, int max) {
		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(ParamServerToDevice.class);
			if (serverToDevice != null) {
				if (StringUtils.isNotEmpty(serverToDevice.getGuid())) {
					criteria.add(Restrictions.eq("guid",
							serverToDevice.getGuid()));
				}
				if (serverToDevice.getCreateDatetime() != null) {
					criteria.add(Restrictions.ge("createDatetime",
							serverToDevice.getCreateDatetime()));
				}
				if (serverToDevice.getFinishDatetime() != null) {
					criteria.add(Restrictions.lt(
							"createDatetime",
							DateUtils.addDays(serverToDevice.getFinishDatetime(), 1)));
				}
				if (serverToDevice.getJobStatus() != null) {
					criteria.add(Restrictions.eq("jobStatus.id", serverToDevice
							.getJobStatus().getId()));
				}
				criteria.addOrder(Order.desc("createDatetime"));
			}
			List<ParamServerToDevice> list = serverToDeviceDao.findByCriteria(
					criteria, first, max);
			int total = serverToDeviceDao.getRowCount(criteria);
			GridDataModel<ParamServerToDevice> page = new GridDataModel<ParamServerToDevice>();
			page.setPage(first / max + 1);
			page.setRows(list);
			page.setTotal(total);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
